import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class V1 {
	
	private static boolean insert(Concept root,Concept c){
		boolean flag = false;
		for(int i=0;i<root.childcount();i++){
				flag =flag || insert(root.getChild(i),c);
		}
		if (flag){
			return true;
		}
		if (root.isChild(c)){
			root.addChild(c);
			c.addParent(root);
			return true;
		}
		return false;
	}
	
	private static void print(Concept c){
		System.out.print(c.string());
		System.out.println("child count:"+c.childcount());
		System.out.println("parent count:"+c.parentcount()+"\n");
		for (int i =0; i< c.childcount();i++){
			print(c.getChild(i));
		}
	}
	
	private static void read(String filename,String allattrlist) throws IOException{
		 FileReader reader = new FileReader(filename);
		 BufferedReader br1 = new BufferedReader(reader);
		 String a;
		 Concept root = new Concept();
		 List<String> temp1 = new ArrayList<String>();
//read attrlist.txt
		 FileReader readattrlist = new FileReader(allattrlist);
		 BufferedReader brattrlist = new BufferedReader(readattrlist);
		 String attrlist;
		 while((attrlist = brattrlist.readLine())!=null){
			temp1.add(attrlist);
		 }
		 	root.addAttribute(temp1);
//read attrlist.txt end
		 while((a = br1.readLine())!=null){
			List<String> temp = new ArrayList<String>();
			String[] array = a.split(",");
			for (int i=0;i<array.length;i++){
				temp.add(array[i]);
			}
//------------------------------------------------------------------			
//			for(int k=0;k<temp.size();k++){
//				System.out.println(temp.get(k));
//				}
			
			
//------------------------------------------------------------------			
			Concept c = new Concept();
			c.addAttribute(temp);
			insert(root,c);
		 }
		 print(root);
		 
	}
	public static void main(String[] args) throws IOException{
		RemoveRepeat_LengthMAXup sortlist= new RemoveRepeat_LengthMAXup();
		sortlist.Asortlist("abcdATTR.txt");

//		for(int k=0;k<sortlist.size();k++){
//		System.out.println(sortlist.get(k));
//		}		
//		for(int i=1;i<sortlist.size();i++){
//	    	String array[]=sortlist.get(i).split(",");
//	    	}
//		for (int i=0;i<array.length;i++){
//			temp.add(array[i]);
//		}
		
		
		
		
		
		
		
		
		
		
		
		read("sortabcdATTR.txt","attrlist.txt");
		
	}

}


